package com.naiterui.ehp.bs.im.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bs.im.config.MqttConnectionConfig;
import com.naiterui.ehp.bs.im.network.mqtt.MqttClientUtils;
import com.naiterui.ehp.bs.im.network.mqtt.handler.ClientStatusHandler;
import com.naiterui.ehp.bs.im.network.protocl.Chater;
import com.naiterui.ehp.bs.im.network.protocl.Message;
import com.naiterui.ehp.bs.im.repository.mongo.MessageDao;
import com.naiterui.ehp.bs.im.utils.Constants;
import com.naiterui.ehp.bs.im.utils.NameUtils;

/**
 * 事件处理器
 */
@Service
public class EventHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(EventHandler.class);

    /**
     * mqtt客户端配置参数类
     */
    private final MqttConnectionConfig mqttConnectionConfig;
    /**
     * 客户端监听器
     */
    private final ClientStatusHandler clientStatusHandler;
    /**
     * 消息数据持久化对象
     */
    private final MessageDao messageDao;

    public EventHandler(MqttConnectionConfig mqttConnectionConfig, ClientStatusHandler clientStatusHandler, MessageDao messageDao) {
        this.mqttConnectionConfig = mqttConnectionConfig;
        this.clientStatusHandler = clientStatusHandler;
        this.messageDao = messageDao;
    }

    /**
     * 公共通知
     */
    public void publicNotice(String message, String userType) {
        String targetTopic = null;
        if (NameUtils.USER_TYPE_DOCTOR.equals(userType)) {
            targetTopic = NameUtils.drPubPushTopic(mqttConnectionConfig.getPublicTopicName());
        } else if (NameUtils.USER_TYPE_PATIENT.equals(userType)) {
            targetTopic = NameUtils.ptPubPushTopic(mqttConnectionConfig.getPublicTopicName());
        }
        MqttClientUtils.pushToAllServers(targetTopic, message);
    }

    /**
     * 私人通知
     */
    public void privateNotice(String message, Long userId, Boolean force, String userType) {

        String targetOnline = null;
        String targetTopic = null;
        Integer relation = null;
        if (NameUtils.USER_TYPE_DOCTOR.equals(userType)) {
            targetOnline = NameUtils.drMsgTopic(userId);
            targetTopic = NameUtils.drPriPushTopic(userId);
            relation = Constants.MESSAGE_RESOURCE_PATIENT;
        } else if (NameUtils.USER_TYPE_PATIENT.equals(userType)) {
            targetOnline = NameUtils.ptMsgTopic(userId);
            targetTopic = NameUtils.ptPriPushTopic(userId);
            relation = Constants.MESSAGE_RESOURCE_DOCTOR;
        } else {
            LOGGER.error("---------- privateNotice ERROR ; illegal userType ");
            return;
        }

        // 非强制
        if (null != force && force) {
            MqttClientUtils.publishMessageToClient(targetTopic, message);
            return;
        }

        // 获取消息接收者的当前在线状态
        boolean isOnline = clientStatusHandler.isOnline(targetOnline);
        if (isOnline) {
            MqttClientUtils.publishMessageToClient(targetTopic, message);
        } else {

            // 保存离线消息
            Message<String> offlineMessage = new Message<String>();
            offlineMessage.setTo(new Chater(userId));
            offlineMessage.setRelation(relation);
            offlineMessage.setType(Constants.MESSAGE_TYPE_TEXT);
            offlineMessage.setContent(message);
            offlineMessage.setSendTime(System.currentTimeMillis());

            messageDao.saveOfflineMessage(offlineMessage);

        }
    }

}
